# WeaponHolder.gd
class_name WeaponHolder
extends Node2D

# 角色朝向
var facing_left: bool = false

# 位置映射
var right_rotation_position_map: Dictionary = {
												  0: Vector2(12, -13),
												  10: Vector2(13, -11),
												  20: Vector2(13, -9),
												  30: Vector2(13, -6),
												  40: Vector2(12, -4),
												  50: Vector2(11, -2),
												  60: Vector2(10, 0),
												  70: Vector2(9, 2),
												  80: Vector2(7, 3),
												  90: Vector2(5, 4),
												  -10: Vector2(11, -15),
												  -20: Vector2(10, -17),
												  -30: Vector2(8, -18),
												  -40: Vector2(6, -19),
												  -50: Vector2(4, -20),
												  -60: Vector2(2, -21),
												  -70: Vector2(0, -21),
												  -80: Vector2(-3, -21),
												  -90: Vector2(-5, -20)
											  }

var left_rotation_position_map: Dictionary = {
												 -90: Vector2(-5, 4),
												 -80: Vector2(-7, 3),
												 -70: Vector2(-9, 2),
												 -60: Vector2(-10, 0),
												 -50: Vector2(-11, -2),
												 -40: Vector2(-12, -4),
												 -30: Vector2(-13, -6),
												 -20: Vector2(-13, -8),
												 -10: Vector2(-13, -10),
												 0: Vector2(-12, -13),
												 10: Vector2(-11, -15),
												 20: Vector2(-10, -17),
												 30: Vector2(-8, -18),
												 40: Vector2(-6, -19),
												 50: Vector2(-4, -20),
												 60: Vector2(-2, -21),
												 70: Vector2(0, -21),
												 80: Vector2(2, -21),
												 90: Vector2(5, -20)
											 }

# 引用当前武器
var current_weapon: WeaponBase = null


func _process(_delta: float) -> void:
	follow_mouse()


func equip_weapon(weapon: WeaponBase) -> void:
	weapon.reparent(self)
	current_weapon = weapon
	# 发射信号并更新武器位置
	follow_mouse()


# 跟随鼠标
func follow_mouse() -> void:
	if not current_weapon:
		return

	# 获取鼠标位置
	var mouse_pos: Vector2 = get_global_mouse_position()

	# 计算方向向量
	var direction: Vector2 = mouse_pos - global_position

	# 计算角度
	var angle_rad: float = direction.angle()
	var angle_deg: float = rad_to_deg(angle_rad)
	if angle_deg < 0:
		angle_deg += 360

	# 计算最终角度
	var final_angle_deg: float = calculate_final_angle(angle_deg, direction)

	# 限制角度范围
	final_angle_deg = clamp(final_angle_deg, -90, 90)

	# 设置武器位置
	var position_map: Dictionary = left_rotation_position_map if facing_left else right_rotation_position_map
	current_weapon.position = interpolate_position(final_angle_deg, position_map)

	# 设置武器旋转
	current_weapon.rotation = deg_to_rad(final_angle_deg)

	# 处理武器翻转
	handle_weapon_flip(current_weapon)


# 计算最终角度
func calculate_final_angle(angle_deg: float, direction: Vector2) -> float:
	var final_angle_deg: float

	if facing_left:
		# 朝左时的角度计算
		if angle_deg >= 90 and angle_deg <= 270:
			final_angle_deg = (angle_deg - 90) * 180 / 180 - 90
		else:
			if direction.y < 0:
				final_angle_deg = 90
			else:
				final_angle_deg = -90
	else:
		# 朝右时的角度计算
		if angle_deg <= 90 or angle_deg >= 270:
			if angle_deg <= 90:
				final_angle_deg = angle_deg
			else:
				final_angle_deg = angle_deg - 360
		else:
			if direction.y < 0:
				final_angle_deg = -90
			else:
				final_angle_deg = 90

	return final_angle_deg


# 插值计算位置
func interpolate_position(target_angle: float, position_map: Dictionary) -> Vector2:
	# 处理边界情况
	if abs(target_angle - 90) < 0.01:
		return position_map[90]
	if abs(target_angle + 90) < 0.01:
		return position_map[-90]

	# 如果有精确匹配
	if position_map.has(int(target_angle)):
		return position_map[int(target_angle)]

	# 找到最近的角度
	var angles: Array = position_map.keys()
	angles.sort()

	var lower_angle: int = -90
	var upper_angle: int = 90

	for angle in angles:
		if angle <= target_angle and angle > lower_angle:
			lower_angle = angle
		if angle >= target_angle and angle < upper_angle:
			upper_angle = angle

	# 处理边界情况
	if lower_angle == -90 and upper_angle == 90:
		return position_map[0] if position_map.has(0) else Vector2.ZERO

	if lower_angle == -90:
		return position_map[upper_angle]
	if upper_angle == 90:
		return position_map[lower_angle]

	# 计算插值
	var t: float = inverse_lerp(float(lower_angle), float(upper_angle), target_angle)
	return position_map[lower_angle].lerp(position_map[upper_angle], t)


# 处理武器翻转
func handle_weapon_flip(weapon: Node) -> void:
	# 如果武器有handle_flip方法，调用它
	if weapon.has_method("handle_flip"):
		weapon.handle_flip(facing_left)
		# 否则尝试找到子节点
	elif weapon.get_child_count() > 0:
		var weapon_sprite: Node = weapon.get_child(0)
		if weapon_sprite is AnimatedSprite2D:
			weapon_sprite.flip_h = facing_left
			weapon_sprite.flip_v = false


# 朝左移动
func _on_walk_walk_to_left() -> void:
	facing_left = true
	update_weapon_flip()


# 朝右移动
func _on_walk_walk_to_right() -> void:
	facing_left = false
	update_weapon_flip()


# 更新武器翻转
func update_weapon_flip() -> void:
	if current_weapon:
		handle_weapon_flip(current_weapon)
